﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Xml.Linq;

namespace iLangl
{
	public class AlertSerializeContext : BaseContext
	{
		protected IAlert _alert;
		public override void SetContext(object context)
		{
			base.SetContext(context);

			if (context is IAlert)
			{
				_alert = context as IAlert;
			}
		}

		public override object GetContext()
		{
			if (_alert == null)
				return string.Empty;

			startDocument();

			writer.WriteStartElement("Alert");

			writer.WriteAttributeString("id", _alert.Id);
			writer.WriteAttributeString("source", _alert.Source);
			writer.WriteAttributeString("severity", _alert.Severity.ToString());
			writer.WriteAttributeString("created", _alert.Created.ToString(DefaultSettingsManager.DateTimeFormat));


			if ((_alert.Errors != null) && (_alert.Errors.Count > 0))
			{
				ErrorsRequestContext context = new ErrorsRequestContext();
				context.SetContext(_alert.Errors);
				context.Write(writer);
			}

			if ((_alert.Exceptions != null) && (_alert.Exceptions.Count > 0))
			{
				writer.WriteStartElement("Exceptions");

				foreach (Exception ex in _alert.Exceptions)
				{
					writer.WriteStartElement("Exception");

					writer.WriteString(GetExceptionDetails(ex));
					writer.WriteEndElement();
				}
				writer.WriteEndElement();
			}

			if ((_alert.Actions != null) && (_alert.Actions.Count > 0))
			{
				writer.WriteStartElement("Actions");
				
				foreach (IAction action in _alert.Actions)
				{
					writer.WriteStartElement("Action");

					writer.WriteAttributeString("id", action.Id);
					writer.WriteAttributeString("actionName", action.ActionName);
					writer.WriteAttributeString("companyId", action.CompanyId);
					writer.WriteAttributeString("contactId", action.ContactId);
					writer.WriteAttributeString("isSynced", action.IsSynced.ToString());
					writer.WriteAttributeString("pluginLiceneKey", action.PluginLiceneKey);
					writer.WriteAttributeString("sessionId", action.SessionId);
					writer.WriteAttributeString("version", action.Version);
					if (action.Container != null)
					{
						writer.WriteAttributeString("packageId", action.Container.Id);
					}

					
					if (action.RequestContext != null)
						writer.WriteRaw(action.RequestContext.GetContext().ToString());

					writer.WriteEndElement();
				}
				writer.WriteEndElement();
			}

			if ((_alert.Packages != null) && (_alert.Packages.Count > 0))
			{
				writer.WriteStartElement("Packages");

				foreach (IPackage package in _alert.Packages)
				{
					writer.WriteStartElement("Package");
					
					writer.WriteAttributeString("id", package.Id);

					writer.WriteRaw(package.PackageBody);
					writer.WriteEndElement();
				}
				
				writer.WriteEndElement();
			}

			writer.WriteEndElement();
			return endDocument();
		}

	


		public string GetExceptionDetails(Exception exception)
		{
			PropertyInfo[] properties = exception.GetType()
						   .GetProperties();
			List<string> fields = new List<string>();
			foreach (PropertyInfo property in properties)
			{
				object value = property.GetValue(exception, null);

				if(value != null)
				fields.Add(String.Format(
								 "{0} = {1}",
									 property.Name,
								 value.ToString()
				));
			}
			return String.Join("\n", fields.ToArray());
		}
	}
}
